// Learn TypeScript:
//  - https://docs.cocos.com/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html

import gameData from "../gameData";
import sceneMediator from "../../framework/sceneMediator";
import mainManage from "../../framework/mainManage";
import GameConfig from "../../framework/GameConfig";
import {SDKManager} from "../SDKManager/SDKManager";

const {ccclass, property} = cc._decorator;

@ccclass
export default class cheatsAction extends cc.Component {

    tweenAction = null;

    @property(cc.Node)
    titleView : cc.Node = null;

    onLoad(){
    
        if(sceneMediator.curEnterLevel == 0){
            this.titleView.getChildByName("bg").getChildByName("showLab").getComponent(cc.Label).string = gameData.getCurTitleString()[sceneMediator.getShowScene() -1] + "";
        }else{
            this.titleView.getChildByName("bg").getChildByName("showLab").getComponent(cc.Label).string = gameData.getPrinceTitleDate()[sceneMediator.princeShowLevel - 1] +"";
        }

        this.node.x = -cc.winSize.width / 2;
        this.tweenAction = cc.tween(this.node).repeatForever(
            cc.tween(this.node)
                .by(10,{position : cc.v2(cc.winSize.width ,0)})
                .by(10,{position : cc.v2(-cc.winSize.width ,0)})
        ).start();
    }

    clickBtnEvent(event,data){
        if(parseInt(data) == 1){
            console.log("秘籍");
            
            if(GameConfig.GetInstance().isLockHide){
                this.tweenAction.stop();
                this.titleView.parent = cc.find("Canvas");
                this.titleView.x = 0;
                this.titleView.y = 0;
                this.titleView.active = true;
                return;
            }

            cc.director.getPhysicsManager().enabled = false;

            let isSeeVideo = false;

            SDKManager.showRewardVideo("秘籍界面",()=>{
                isSeeVideo = true;
                GameConfig.GetInstance().isLockHide = true;
                this.tweenAction.stop();

                this.titleView.parent = cc.find("Canvas");
                this.titleView.x = 0;
                this.titleView.y = 0;
                this.titleView.active = true;

            },()=>{
                cc.director.getPhysicsManager().enabled = true;
                if(isSeeVideo){
                    mainManage.createToast("Hadiahnya akan diberikan setelah menonton video ini");
                }else{
                    mainManage.createToast("Tidak ada iklan yang tersedia saat ini");
                }
            })
        }

        if(parseInt(data) == 2){
            cc.director.getPhysicsManager().enabled = true;
            this.titleView.active = false;
            cc.game.emit("clickTitle");
            cc.game.emit("showTitleEvent");
            this.node.destroy();
        }
    }
}
